package com.site.action.backend.system;

import java.util.List;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSONObject;
import com.site.dto.backend.system.AuthorityBizBean;
import com.site.service.backend.system.AuthService;
import com.site.util.base.action.BackendBaseAction;
import com.site.util.base.service.BaseService;
import com.site.util.exception.ApplicationException;


public class AuthAction extends BackendBaseAction {

	private static final long serialVersionUID = 1L;

	private AuthService authService;

	private AuthorityBizBean authorityBizBean;
	
	private List<AuthorityBizBean> authorityBizBeanList;
	/*
	 * 权限ids
	 */
	private String ids;
	
	private String idsStr;

	private List<Long> authIdList;
	
	public String getAuthList(){
		if (queryPage == null) {
			initPagination();
		}
		authorityBizBeanList = authService.getAuthList(queryPage, authorityBizBean);
		return "getAuthList";
	}
	
	/**
	 * 保存权限
	 * 
	 * @return
	 */
	public String save() throws ApplicationException,Exception {
		try {
			if (authorityBizBean.getName()== null || "".equals(authorityBizBean.getName().trim())) {
				response.getWriter().print("{\"msg\":\"权限名称不能为空!\"}");
				log.info("验证权限失败,权限名称不能为空……");
			} else if (authService.getAuthByName(authorityBizBean.getName()) != null){
				response.getWriter().print("{\"msg\":\"权限已存在……\"}");
				log.info("验证权限失败,权限已存在……");
			} else if (getCurrentUser() != null) {
				authorityBizBean.setCreatedUser(getCurrentUser().getUserName());
				authorityBizBean.setUpdatedUser(getCurrentUser().getUserName());
				authService.execute(authorityBizBean, BaseService.COMMAND_SAVE);
				response.getWriter().print("{\"msg\":\"\"}");
				log.info("权限增加……");
			}
		} catch (ApplicationException e) {
			throw (e);
		}
		return null;
	}

	/**
	 * 更新权限
	 * 
	 * @return
	 */
	public String update()throws ApplicationException, Exception  {
		try {
			if(null != authorityBizBean && authorityBizBean.getId() !=null){
				authService.execute(authorityBizBean, BaseService.COMMAND_UPDATE);
				response.getWriter().print("{\"msg\":\"\"}");
				log.info("权限更新成功……");
			} else {
				response.getWriter().print("{\"msg\":\"权限Id为空!\"}");
				log.info("权限id不能为空……");
			}
		} catch (ApplicationException e) {
			throw (e);
		}
		return null;
	}
	/**
	 * 更新权限页面
	 * @return
	 */
	public String updatePage()throws Exception{
		if (queryPage == null) {
			initPagination();
		}
		
		if(null != authorityBizBean  && authorityBizBean.getId() != null){
			authorityBizBeanList = authService.getAuthList(queryPage, authorityBizBean);
			if(null != authorityBizBeanList && authorityBizBeanList.size() == 1){
				log.info(JSONObject.toJSONString(authorityBizBeanList.get(0)));
				response.getWriter().print(JSONObject.toJSONString(authorityBizBeanList.get(0)));
			}
			log.info("权限获得成功，正在返回……");
		} else {
			response.getWriter().print("{\"msg\":\"\"}");
			log.info("权限id获得失败……");	
		}
		return null;
	}
	
	/**
	 * 物理删除权限
	 * @return
	 */
	public String authDelete(){
		
		try {
    		
    		if(null != authIdList && authIdList.size() > 0) {
        		String name = (String)authService.execute(authIdList, BaseService.COMMAND_DELETE);
        		super.setParams(name);
        		super.setMessageID("SYS_MSG_00022");
        	} else {
        		log.info("编号为空。。。。。");
				super.setMessageID("SYS_MSG_00026");
        	}
    		
    	} catch(Exception e) {
    		super.setMessageID("SYS_MSG_00026");
    		e.printStackTrace();
    	}
    	
    	super.setBackurl(request.getContextPath() + "/system/authAction!getAuthList.do");
		return "toMessage";
	}
	
	
	/**
	 * 删除权限(逻辑删除即禁用)
	 * @return
	 */
	public String disabled()throws Exception{
		try {
			if(null != idsStr && idsStr.length() > 0) {
				authService.execute(idsStr, "disabled");
				response.getWriter().print("{\"message\":\"禁用成功!\"}");
			} else {
				log.info("禁用失败,id为空!");
				response.getWriter().print("{\"message\":\"禁用失败!\"}");
			}
		} catch (ApplicationException e) {
			throw (e);
		}
		return null;
	}
	
	/**
	 * 启用角色(启用)
	 * @return
	 */
	public String enabled()throws Exception{
		try {
			
			if(null != idsStr && idsStr.length() > 0) {
				authService.execute(idsStr, "enabled");
				response.getWriter().print("{\"message\":\"启用成功!\"}");
			} else {
				log.info("启用失败,id为空!");
				response.getWriter().print("{\"message\":\"启用失败!\"}");
			}
		} catch (ApplicationException e) {
			throw (e);
		}
		return null;
	}
	
	@Resource
	public void setAuthService(AuthService authService) {
		this.authService = authService;
	}

	public AuthService getAuthService() {
		return authService;
	}

	public void setAuthorityBizBean(AuthorityBizBean authorityBizBean) {
		this.authorityBizBean = authorityBizBean;
	}

	public AuthorityBizBean getAuthorityBizBean() {
		return authorityBizBean;
	}
	public void setAuthorityBizBeanList(List<AuthorityBizBean> authorityBizBeanList) {
		this.authorityBizBeanList = authorityBizBeanList;
	}
	public List<AuthorityBizBean> getAuthorityBizBeanList() {
		return authorityBizBeanList;
	}
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}

	public List<Long> getAuthIdList() {
		return authIdList;
	}

	public void setAuthIdList(List<Long> authIdList) {
		this.authIdList = authIdList;
	}

	public String getIdsStr() {
		return idsStr;
	}

	public void setIdsStr(String idsStr) {
		this.idsStr = idsStr;
	}
	 
}
